package com.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.IBisDataDao;
import com.dao.IBisOpeDao;
import com.dao.IRetPrdInfoDao;
import com.model.Bis_data;
import com.model.Bis_ope;
import com.model.Ret_prd_in;
import com.model.Ret_prd_indt;
import com.model.Ret_prd_info;
import com.rtnBean.logoff.NxOpeAll;
import com.util.Util;

import static com.def.GenericDef.*;

@Repository
public class RetPrdInfoHibernateDao extends
		GenericHibernateDao<Ret_prd_info, String> implements IRetPrdInfoDao {

	@Autowired
	IBisOpeDao bisOpeDao;
	@Autowired
	IBisDataDao bisDataDao;
	
	public Ret_prd_info setNxOpeInfo(Ret_prd_info prdInfo, NxOpeAll nxOpeAll) {
		if (null != nxOpeAll.getNxOpe()) {
			// pv
			// prdInfo.setPv_path_id_fk(prdInfo.getCr_path_id_fk());
			// prdInfo.setPv_path_ver_fk(prdInfo.getCr_path_ver_fk());
			// prdInfo.setPv_ope_id_fk(prdInfo.getCr_ope_id_fk());
			// prdInfo.setPv_ope_no_fk(prdInfo.getCr_ope_no_fk());
			// prdInfo.setPv_ope_ver_fk(prdInfo.getCr_ope_ver_fk());

			// cr
			// prdInfo.setCr_path_id_fk(prdInfo.getNx_path_id_fk());
			// prdInfo.setCr_path_ver_fk(prdInfo.getNx_path_ver_fk());
			// prdInfo.setCr_ope_no_fk(prdInfo.getNx_ope_no_fk());
			// prdInfo.setCr_ope_id_fk(prdInfo.getNx_ope_id_fk());
			// prdInfo.setCr_ope_ver_fk(prdInfo.getNx_ope_ver_fk());

			// nx
			prdInfo.setNx_path_id_fk(nxOpeAll.getNxPathi().getId().getPath_id());
			prdInfo.setNx_path_ver_fk(nxOpeAll.getNxPathi().getId()
					.getPath_ver());
			prdInfo.setNx_ope_no_fk(nxOpeAll.getNxPath().getId().getCr_ope_no());
			prdInfo.setNx_ope_id_fk(nxOpeAll.getNxOpe().getId().getOpe_id());
			prdInfo.setNx_ope_ver_fk(nxOpeAll.getNxOpe().getId().getOpe_ver());
			prdInfo.setNx_toolg_id_fk(nxOpeAll.getNxOpe().getToolg_id());
			prdInfo.setNx_tool_id_fk(_SPACE);

			prdInfo.setPv_prd_stat_fk(prdInfo.getPrd_stat());
			prdInfo.setPrd_stat(PROC_WAIT);
			prdInfo.setEvt_cate(EVT_LGOF);

		} else {// COMP
			prdInfo.setNx_path_id_fk(_SPACE);
			prdInfo.setNx_path_ver_fk(_SPACE);
			prdInfo.setNx_ope_no_fk(_SPACE);
			prdInfo.setNx_ope_id_fk(_SPACE);
			prdInfo.setNx_ope_ver_fk(_SPACE);
			prdInfo.setNx_toolg_id_fk(_SPACE);
			prdInfo.setNx_tool_id_fk(_SPACE);

			prdInfo.setPv_prd_stat_fk(prdInfo.getPrd_stat());
			prdInfo.setPrd_stat(PROC_COMPLETE);
			prdInfo.setEvt_cate(EVT_LGOF);

		}
		return prdInfo;
	}

	public List<Ret_prd_info> getShtListInfo(String boxId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk ='" + boxId + "'";
		prdInfoList = this.find(querySql);
		if (prdInfoList.isEmpty()) {
			return null;
		}
		return prdInfoList;
	}
	public List<Ret_prd_info> getShtListInfoWithWo(String boxId, String woId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk = '" + boxId + "' and wo_id_fk = '"+woId+"'";
		prdInfoList = this.find(querySql);
		if (prdInfoList.isEmpty()) {
			return null;
		}
		return prdInfoList;
	}
	public List<Ret_prd_info> getShtListInfoWithLock(String boxId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		// prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
		String hql = "FROM Ret_prd_info where box_id_fk = ?";
		prdInfoList = this.listWithLock(hql, boxId);
		if (prdInfoList.isEmpty()) {
			return null;
		}
		return prdInfoList;
	}

	public List<Ret_prd_info> getShtListInfoOrderBySlot(String boxId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk ='" + boxId
				+ "' ORDER BY slot_no * 1";
		prdInfoList = this.find(querySql);
		return prdInfoList;
	}

	public List<Ret_prd_info> getShtListInfoOrderByP4Time(String boxId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk ='" + boxId
				+ "' ORDER BY p4_timestamp asc";
		prdInfoList = this.find(querySql);
		return prdInfoList;
	}
	
	public List<Ret_prd_info> getShtListInfoOrderBySlotDesc(String boxId) {
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk ='" + boxId
				+ "' ORDER BY slot_no * 1 DESC";
		prdInfoList = this.find(querySql);
		return prdInfoList;
	}

	public int getOkPrdCnt(String boxId) {
		String querySql = "SELECT COUNT(*) FROM Ret_prd_info where box_id_fk ='"
				+ boxId + "' AND prd_grade = '" + _OK + "' ";
		return (int) this.getCountBySql(querySql);
	}

	public int getNgPrdCnt(String boxId) {
		String querySql = "SELECT COUNT(*) FROM Ret_prd_info where box_id_fk ='"
				+ boxId + "' AND prd_grade = '" + _NG + "' ";
		return (int) this.getCountBySql(querySql);
	}

	public int getScrpPrdCnt(String lot_id) {
		String querySql = "SELECT COUNT(*) FROM Ret_prd_info where prd_stat ='SCRP' AND lot_id = '"
				+ lot_id + "' ";
		return (int) this.getCountBySql(querySql);
	}
	/******** 自动填充断掉的子格位号 *********/
	public String getNxSlotNoLoop(String box_id) {
		String nxSlotNoS = "";
		String sqlStr = "SELECT SLOT_NO FROM RET_PRD_INFO T WHERE T.BOX_ID_FK ='"
				+ box_id + "' ORDER BY SLOT_NO ASC";
		List<Object> slotO = this.findBySQL(sqlStr);
		if (slotO.isEmpty()) {
			nxSlotNoS = "1";
		} else {
			for (int slot_no = 1;; slot_no++) {
				for (int i = 0; i < slotO.size(); i++) {
					int slotOi = Integer.valueOf(slotO.get(i).toString());
					if (slot_no == slotOi) {
						break;
					}
					if (i == slotO.size() - 1) {
						nxSlotNoS = Integer.valueOf(slot_no).toString();
						return nxSlotNoS;
					}
				}

			}

		}
		return nxSlotNoS;
	}
	
	public Boolean hasUserInputSlot(String box_id,
			String inputSlotNo) {
		List<Ret_prd_indt> ret_prd_indtList = new ArrayList<Ret_prd_indt>();
		String querySql = "FROM Ret_prd_info where box_id_fk ='"
				+ box_id
				+ "' and slot_no = '" + inputSlotNo + "'";
		ret_prd_indtList = this.find(querySql);
		if (ret_prd_indtList.isEmpty()) {
			return false;
		}
		return true;
	}

	public Boolean isChangeProcFlg(Ret_prd_info retPrdInfo) {
		String crOpeId,crOpeVer,crProcCate,nxOpeId,nxOpeVer,nxProcCate;
		String hql = "FROM Bis_ope where ope_id = ?  and ope_ver = ?";
		crOpeId = retPrdInfo.getCr_ope_id_fk();
		crOpeVer = retPrdInfo.getCr_ope_ver_fk();
		nxOpeId = retPrdInfo.getNx_ope_id_fk();
		nxOpeVer = retPrdInfo.getNx_ope_ver_fk();
		Bis_ope crBisOpe = bisOpeDao.uniqueResult(hql, crOpeId,crOpeVer); 
		Bis_ope nxBisOpe = bisOpeDao.uniqueResult(hql, nxOpeId,nxOpeVer);
		String hql2 = "FROM Bis_data where data_cate = 'PRST' and data_ext = ?";
		Bis_data crProc = bisDataDao.uniqueResult(hql2, crBisOpe.getProc_id()); 
		Bis_data nxProc = bisDataDao.uniqueResult(hql2, nxBisOpe.getProc_id()); 
		if(!crProc.getExt_4().equals(nxProc.getExt_4())){
			return true;
		}
		return false;
	}
	
	public int getrlPrdCnt(String woId) {
		String querySql = "SELECT COUNT(*) FROM Ret_prd_info where wo_id_fk ='" + woId+ "' ";
		return (int) this.getCountBySql(querySql);
	}
	public boolean hasUserInputSlotInBox(String box_id,
			String inputSlotNo) {
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		String querySql = "FROM Ret_prd_info where box_id_fk = ? and  slot_no = ?";
		ret_prd_infoList = this.list(querySql,box_id,inputSlotNo);
		if (ret_prd_infoList.isEmpty()) {
			return false;
		}
		return true;
	}
}
